package cn.fintecher.pangolin.service.dataimp.task;

import cn.fintecher.pangolin.common.enums.ImportDataExcelStatus;
import cn.fintecher.pangolin.common.model.UserModel;
import cn.fintecher.pangolin.common.utils.ZWDateUtil;
import cn.fintecher.pangolin.entity.elastic.DataImportRecord;
import cn.fintecher.pangolin.entity.elastic.Warning;
import cn.fintecher.pangolin.entity.elastic.WarningModel;
import cn.fintecher.pangolin.service.dataimp.elastic.DataImportRecordElastic;
import cn.fintecher.pangolin.service.dataimp.elastic.WarningElastic;
import cn.fintecher.pangolin.service.dataimp.elastic.WarningModelElastic;
import cn.fintecher.pangolin.common.utils.RepositoryUtil;
import org.apache.commons.collections4.IterableUtils;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @Auther: xiaqun
 * @Description:
 * @Date: 16:46 2019/11/5
 */

@Service("cancelWarningTask")
public class CancelWarningTask {
    @Autowired
    private WarningElastic warningElastic;

    @Autowired
    private WarningModelElastic warningModelElastic;

    @Autowired
    private DataImportRecordElastic dataImportRecordElastic;

    /**
     * 撤销警告信息导入
     */
    @Async
    @Transactional
    public void cancelWarning(BoolQueryBuilder builder, DataImportRecord dataImportRecord, UserModel userModel) {
        Iterable<WarningModel> warningModels = warningModelElastic.search(builder);
        Iterable<Warning> warnings = warningElastic.search(builder);
        warningModels.forEach(e -> e.setFlag(1));
        warnings.forEach(e -> e.setFlag(1));
        RepositoryUtil.saveData(warningModelElastic, IterableUtils.toList(warningModels));
        RepositoryUtil.saveData(warningElastic, IterableUtils.toList(warnings));
        dataImportRecord.setStatus(ImportDataExcelStatus.BILL_REVOKE);
        dataImportRecord.process(userModel.getRealName(), ZWDateUtil.getNowDateTime());
        dataImportRecordElastic.save(dataImportRecord);
    }
}
